<template>
  <div>
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="恭喜你看到了底线"
      @load="onLoad"
    >
      <van-cell
        v-for="item in myList"
        :key="item.filmId"
        @click="handelRouter(item.filmId)"
      >
        <div class="varCellitem">
          <img :src="item.poster" alt="" />
          <div class="info">
            <div class="title">
              {{ item.name }} <em>{{ item.item.name }}</em>
            </div>
            <div :class="item.grade ? '' : 'gradeNull'">
              观众评分：<span>{{ item.grade | gradeHandle }}</span>
            </div>
            <div class="actors">主演：{{ item.actors | actorsHandle }}</div>
            <div>{{ item.nation }} | {{ item.runtime }}分钟</div>
          </div>
          <a href="javascript:;">约票</a>
        </div>
      </van-cell>
    </van-list>
  </div>
</template>

<script>
import Vue from 'vue'
// 引入全局的状态和方法
import { mapState } from 'vuex'
// 引入封装好的axios
import http from '@/util/http'
Vue.filter('gradeHandle', (data) => {
  return data || '暂无评分'
})
Vue.filter('actorsHandle', (data) => {
  if (!data) return '暂无主演'
  return data.map((item) => item.name).join('')
})
export default {
  data () {
    return {
      myList: [],
      loading: false,
      finished: false,
      index: 1,
      total: 0
    }
  },
  computed: {
    // 把全局的状态解构出来
    ...mapState(['cityId'])
  },
  mounted () {
    // 异步更新数据
    // 请求数据去猫眼网 用封装好的axios
    http({
      url: `gateway?cityId=${this.cityId}&pageNum=${this
        .index++}&pageSize=10&type=1&k=946385`,
      headers: {
        'X-Host': 'mall.film-ticket.film.list'
      }
    }).then((res) => {
      this.myList.push(...res.data.data.films)
    })
  },
  methods: {
    handelRouter (id) {
      this.$router.push(`/listDetails/${id}`)
    },
    onLoad () {
      // 如果这个数组的数量大于 后端给的总数就停止加载
      if (this.myList.length >= this.total && this.total != 0) {
        this.finished = true
        return
      }
      // 异步更新数据
      // 请求数据去猫眼网 用封装好的axios
      http({
        url: `gateway?cityId=${this.cityId}&pageNum=${this
          .index++}&pageSize=10&type=1&k=946385`,
        headers: {
          'X-Host': 'mall.film-ticket.film.list'
        }
      }).then((res) => {
        this.myList.push(...res.data.data.films)
        // 加载状态结束
        this.loading = false
        // 把后端传来的值赋值给前端
        this.total = res.data.data.total
      })
    }
  }
}
</script>

<style lang="scss" scoped>
$textColor: #797d82;
$themeColor: #ffb232;
.van-list {
  background: #fff;
  margin-top: 10px;
}
.varCellitem {
  display: flex;
  height: 7.75rem;
  align-items: center;
  font-size: 13px;
  line-height: 1.5;
  img {
    width: 4.125rem;
    height: 5.8125rem;
  }
  .info {
    width: 14rem;
    height: 5.125rem;
    padding: 0 0.625rem;
    color: $textColor;
    span {
      color: $themeColor;
    }
  }
  a {
    width: 3.4375rem;
    height: 1.5625rem;
    border: 0.0625rem solid $themeColor;
    color: $themeColor;
    line-height: 1.5625rem;
    text-align: center;
    text-decoration: none;
  }
  .title {
    width: 100%;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    font-size: 16px;
    color: #333;
    em {
      display: inline-block;
      height: 14px;
      background: #d2d6dc;
      color: #fff;
      font-style: normal;
      font-size: 12px;
      line-height: 14px;
      padding: 0 3px;
      border-radius: 2px;
    }
  }
  .actors {
    width: 100%;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
}
</style>
